package com.shiwenping.reply.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.support.design.widget.FloatingActionButton;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.shiwenping.reply.R;
import com.shiwenping.reply.tools.Path;
import com.shiwenping.reply.tools.sp.SharedPreferenceUtil;

import static android.support.design.widget.FloatingActionButton.SIZE_NORMAL;

public class ToastService extends Service {
    public ToastService() {
    }


    private WindowManager.LayoutParams params;
    private WindowManager windowManager;
    private View view;

    @Override
    public void onCreate() {
        super.onCreate();
        params = new WindowManager.LayoutParams();
        windowManager = (WindowManager) getApplication().getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_TOAST;
        } else {
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        params.format = PixelFormat.RGBA_8888;
        params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;


        params.gravity = SharedPreferenceUtil.get(getApplicationContext(), Path.KEY_IS_TOAST_LEFT, true) ? Gravity.LEFT : Gravity.RIGHT;
        params.x = 25;
        params.y = 0;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;

        LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
        view = layoutInflater.inflate(R.layout.toast, null, false);
        view.setOnClickListener(view -> Toast.makeText(ToastService.this, "测试啦", Toast.LENGTH_SHORT).show());
        windowManager.addView(view, params);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(TOAST_OPEN_ACTION);
        intentFilter.addAction(TOAST_STOP_ACTION);
        intentFilter.addAction(TOAST_IS_LEFT);
        registerReceiver(broadcastReceiver, intentFilter);
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(broadcastReceiver);
        super.onDestroy();
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (TOAST_OPEN_ACTION.equals(intent.getAction())) {
                if (windowManager != null) {
                    windowManager.updateViewLayout(view, params);
                }
            } else if (TOAST_STOP_ACTION.equals(intent.getAction())) {
                if (windowManager != null) {
                    windowManager.removeView(view);
                }
            } else if (TOAST_IS_LEFT.equals(intent.getAction())) {
                if (windowManager != null) {
                    params.gravity = intent.getBooleanExtra("isLeft", false) ? Gravity.LEFT : Gravity.RIGHT;
                    windowManager.updateViewLayout(view, params);
                }
            }
        }
    };


    public static final String TOAST_STOP_ACTION = "close_toast";
    public static final String TOAST_OPEN_ACTION = "open_toast";
    public static final String TOAST_IS_LEFT = "is_left_toast";


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
